package android.javaClasses;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.moodle.GameActivity;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: michelle
 * Date: 4/27/11
 * Time: 9:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataHelper {
    private static final String DB_NAME = "moodle";
    private static final int DB_VERSION = 1;
    private static final String TABLE_NAME ="scores";
    private static final String COL_PLAYER1 ="Player1";
    private static final String COL_PLAYER2 ="Player2";
    private static final String COL_PLAYER3 ="Player3";
    private static final String WINNER ="Winner";

    private Context context;
    private SQLiteDatabase db;

    private SQLiteStatement insertStm;
    private static final String INSERT = "INSERT INTO " + TABLE_NAME + " (" +
            COL_PLAYER1 + ", " + COL_PLAYER2 + ", " + COL_PLAYER3 + ", "
        + WINNER +") VALUES (?, ?, ?, ?)";

    public DataHelper(Context context) {
        this.context = context;
        OpenHelper openHelper = new OpenHelper(this.context);
        this.db = openHelper.getWritableDatabase();
        this.insertStm = this.db.compileStatement(INSERT);

    }

    /**Adds data to a new row. Returns the row ID if successful. -1 if not*/
    public long insert(Game aGame) {
        String winner = aGame.getWinner();
        ArrayList<String> players = aGame.getPlayerList();
        String player = "";
        for(int i=0; i< players.size(); i++) {
            if(players.size() > 2) {
                player = players.get(i);
                insertStm.bindString(i+1, player);
            } else {
                player = players.get(i);
                insertStm.bindString(i+1, player);
                insertStm.bindString(3, " "); //Adds an empty value, since there's only 2 players
            }
        }
        Log.i("WINNER", winner);
        insertStm.bindString(4, winner);
        return insertStm.executeInsert();
    }

    public void deleteAll() {
        db.delete(TABLE_NAME, null, null);
    }

    public ArrayList<Game> selectAll() {
        ArrayList<Game> games = new ArrayList<Game>();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        String player1 ="";
        String player2 ="";
        String player3 ="";
        String winner ="";
        String res ="";
        if(cursor.moveToFirst()) {
            do {
                player1 = cursor.getString(1);
                player2 = cursor.getString(2);
                player3 = cursor.getString(3);
                winner = cursor.getString(4);
                Log.i("selectAll", winner);
                if(player3.equals(" ")) res = player1 + " vs. " + player2;
                else res = player1 + " vs. " + player2 + " vs. " + player3;
                Game aGame = new Game(res, winner);
                games.add(aGame);
            } while(cursor.moveToNext());

        }
        if(cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return games;
    }

    private static class OpenHelper extends SQLiteOpenHelper {

        OpenHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_PLAYER1 + " TEXT NOT NULL, " + COL_PLAYER2 + " TEXT NOT NULL, "
                    + COL_PLAYER3 + " TEXT, " + WINNER + " TEXT NOT NULL)");
        }

        /**
         * Upgrading database, tables will be dropped and recreate the db
         * @param db
         * @param oldVersion
         * @param newVersion
         */

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(DB_NAME, "Upgrading db");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

}